from conans import ConanFile, tools
from conan.tools.cmake import CMakeToolchain, CMakeDeps, CMake
from conan.tools.layout import cmake_layout
import os

class SparkMaxConan(ConanFile):
    name = "sparkmax"
    exports_sources = "CMakeLists.txt"

    # Optional metadata
    license = "BSD style"
    homepage = "https://www.revrobotics.com/"
    url = "https://github.com/conan-io/conan-center-index"
    description = "A library for interacting with Rev Robotics SparkMax motor controllers"
    topics = ("frc", "robotics", "rev", "first", "rev-robotics")

    # Binary configuration
    settings = "os", "compiler", "build_type", "arch"
    options = { "shared": [True, False] }
    default_options = { "shared": False }

    @property
    def _source_subfolder(self):
        return "source_subfolder"

    @property
    def _build_subfolder(self):
        return "build_subfolder"

    def requirements(self):
        """Pull wpilib requirements"""
        self.requires("allwpilib/2022.1.1")

    def source(self):
        tools.download(self.conan_data["sources"][self.version]["url"], "SparkMax.zip")
        tools.unzip("SparkMax.zip", ".")
        tools.unzip(
            #os.path.join(
                "maven/com/revrobotics/frc/SparkMax-cpp/%s/" % self.version
            #)
            + "SparkMax-cpp-%s-headers.zip" % self.version,
            "."
        )
        tools.unzip(
            #os.path.join(
                "maven/com/revrobotics/frc/SparkMax-cpp/%s/" % self.version
            #)
            + "SparkMax-cpp-%s-source.zip" % self.version,
            "."
        )

    def generate(self):
        on_off = lambda v: "ON" if v else "OFF"
        toolchain = CMakeToolchain(self)
        toolchain.generate()
        deps = CMakeDeps(self)
        deps.generate()

    def layout(self):
        cmake_layout(self)

    def build(self):
        """Call CMake build command"""
        cmake = CMake(self)
        cmake.configure()
        cmake.build()

    def package(self):
        """Use CMake's built in install functionality to package outputs"""
        self.copy(pattern="LICENSE.txt", dst="LICENSE", src=self._build_subfolder)
        cmake = CMake(self)
        cmake.configure()
        cmake.install()

    def package_info(self):
        """Data about libraries within package"""
        self.cpp_info.names["cmake_find_package"] = "SparkMax"
        self.cpp_info.names["cmake_find_package_multi"] = "SparkMax"

        libsufix = "" if self.settings.build_type == "Debug" else ""

        self.cpp_info.includedirs = [""]
        self.cpp_info.libdirs = [""]
        self.cpp_info.libs = ["SparkMax" + libsufix]
